const ModelCarousel = require('../../models/carousel'),
    FileTool = require('../../utils/filetool'),
    ComTool = require('../../utils/commonTools');

const fileSavePath = "/images/carousel_cover_url";
// 删除轮播图
exports.delCarousel = (req, res, next) => {
    const {
        carousel_id,
        carousel_file_src,
        carousel_file_name
    } = req.query;
    ModelCarousel.delSinger({
        carousel_id
    }, (value) => {
        if (carousel_file_src && carousel_file_name)
            FileTool.removeFile('./public' + carousel_file_src, carousel_file_name);
        res.render('success', {
            data: JSON.stringify({
                message: "删除成功",
                status: 200,
                affectedRows: value.affectedRows
            })
        })
    }, (error) => {
        res.render('fail', {
            data: JSON.stringify({
                message: "删除失败",
                status: 200,
                error
            })
        })
    });
}
// 编辑轮播图
exports.updateCarousel = async (req, res, next) => {
    const {
        carousel,
        file
    } = req.body;
    let newData = file ? await FileTool.saveImage(file, './public' + fileSavePath) : null;
    if (newData === false) {
        return res.render('fail', {
            data: JSON.stringify({
                message: "图片保存失败",
                status: 200,
            })
        })
    } else {
        if (carousel.carousel_file_src && carousel.carousel_file_name)
            FileTool.removeFile('./public' + carousel.carousel_file_src, carousel.carousel_file_name);
    }

    let Data = {
        ...carousel,
        create_time:ComTool.getCurrentTime(),
        carousel_file_src: newData ? fileSavePath + '/' + newData.saveFloder + '/' : null,
        carousel_file_name: newData ? newData.name : null,
    }

    const {
        carousel_id
    } = Data;
    if (carousel_id) {
        delete Data.carousel_id;
        ModelCarousel.updateCarousel(Data, {
            carousel_id
        }, (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "更新成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            if (Data.carousel_file_src && Data.carousel_file_name)
                FileTool.removeFile('./public' + Data.carousel_file_src, Data.carousel_file_name);
            res.render('fail', {
                data: JSON.stringify({
                    message: "更新失败",
                    status: 200,
                    error
                })
            })
        });
    }
}

/** 添加轮播图 */
exports.addCarousel = async (req, res, next) => {
    const {
        carousel,
        file
    } = req.body;
    let newData = file ? await FileTool.saveImage(file, './public' + fileSavePath) : null;

    if (newData === false) {
        return res.render('fail', {
            data: JSON.stringify({
                message: "图片保存失败",
                status: 200,
            })
        })
    }

    let Data = {
        ...carousel,
        create_time:ComTool.getCurrentTime(),
        carousel_file_src: newData ? fileSavePath + '/' + newData.saveFloder + '/' : null,
        carousel_file_name: newData ? newData.name : null,
    }
    ModelCarousel.addCarousel(Data,
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "添加成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            if (Data.carousel_file_src && Data.carousel_file_name)
                FileTool.removeFile('./public' + Data.carousel_file_src, Data.carousel_file_name);
            res.render('fail', {
                data: JSON.stringify({
                    message: "添加失败",
                    status: 200,
                    error
                })
            })
        });
}
/** 查找轮播图 */
exports.findCarousel = async (req, res, next) => {
    let Carousel = await ModelCarousel.findCarousel(req.body);
    Carousel.success((value) => {
        if (value.length) {
            res.render('success', {
                data: JSON.stringify({
                    message: "查找到轮播图",
                    value: value,
                    status: 200,
                    length: value.length
                })
            })
        } else {
            res.render('fail', {
                data: JSON.stringify({
                    message: "没有找到轮播图",
                    value: value,
                    length: value.length
                })
            })
        }
    });

    Carousel.fail((error) => {
        res.render('fail', {
            data: JSON.stringify({
                message: "查找失败",
                error
            })
        })
    })
}

//随机查找8张轮播图
exports.findRand = (req, res, next) => {
    ModelCarousel.findRand(
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "查找成功",
                    status: 200,
                    value: value,
                    length: value.length
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "查找失败",
                    status: 200,
                    error
                })
            })
        });
}

//查找所有轮播图
exports.list = (req, res, next) => {
    const {
        isAll
    } = req.body;

    ModelCarousel.findAll(
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "查找成功",
                    status: 200,
                    value: value,
                    length: value.length
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "查找失败",
                    status: 200,
                    error
                })
            })
        }, isAll);
}